/*
 * CourseWindow.cpp
 *
 *  Created on: 18/06/2011
 *      Author: dela
 */

#include "CourseWindow.h"

#include <string>
#include <sstream>
#include <iostream>
#include <list>
#include "../Connection.h"
#include "common/Class.h"
#include "ClassWindow.h"

using namespace std;

/**
 *
 */
CourseWindow::CourseWindow(BaseObjectType* cobject, const Glib::RefPtr<Gtk::Builder>& builder) :
	Gtk::Window(cobject), builder(builder) {
	c.setId(0);

	// TreeView:
	Gtk::TreeView *treeView;
	builder->get_widget("treeview1", treeView);
	treeModel = Gtk::ListStore::create(mColumns);
	treeView->set_model(treeModel);

	// Boton guardar y salir:
	Gtk::Button *button = 0;
	builder->get_widget("button1", button);
	button->signal_clicked().connect(
			sigc::mem_fun(*this, &CourseWindow::onSave));

	// Boton Cancelar:
	builder->get_widget("button2", button);
	button->signal_clicked().connect(
			sigc::mem_fun(*this, &CourseWindow::hide));

	// OnShow:
	this->signal_show().connect(
			sigc::mem_fun(*this, &CourseWindow::onShow));

	// Accion al hacer doble click:
	treeView->signal_row_activated().connect(
			sigc::mem_fun(*this, &CourseWindow::onRowSelected));

	// Refrescar cuando gana foco la ventan:
	this->signal_focus_in_event().connect(
			sigc::mem_fun(*this, &CourseWindow::onFocus));
}


/**
 *
 */
bool CourseWindow::onFocus(GdkEventFocus* e) {
	setTreeModelInfo();
	return true;
}


/**
 *
 */
void CourseWindow::setTreeModelInfo() {
	treeModel->clear();

	if (c.getId() != 0) {
		Gtk::TreeModel::Row row;
		classes.clear();
		Connection::getInstance()->getClasses(classes);

		list<Class>::iterator it;
		for (it=classes.begin(); it!=classes.end(); it++) {
			if ( it->getCourseId() == c.getId() ) {
				row = *(treeModel->append());
				row[mColumns.id] = it->getId();
				row[mColumns.type] = "Clase";
				row[mColumns.name] = it->getName();
				row[mColumns.startDate] = it->getStartDate();
			}
		}
	}
}


/**
 *
 */
void CourseWindow::onRowSelected(const Gtk::TreeModel::Path &path,
		Gtk::TreeViewColumn *column) {
	Gtk::TreeModel::iterator iter = treeModel->get_iter(path);
	Gtk::TreeModel::Row row = *iter;
	unsigned long idSelected = row[mColumns.id];
	list<Class>::iterator it;
	Class *c;
	for (it=classes.begin(); it!=classes.end(); it++) {
		if (it->getId() == idSelected) {
			c = &(*it);
		}
	}

	// Verify password:
	bool pass = false;
	if (c->getHasPassword()) {
		//Dialogo de ingreso de password:
		Gtk::Dialog *dialog;
		builder->get_widget("dialog1", dialog);
		Gtk::Entry *entryPass;
		builder->get_widget("entry6", entryPass);
		entryPass->set_text("");
		int ret = dialog->run();
		if ( ret == 1 && c->getPassword() == entryPass->get_text() ) {
			pass = true;
		} else {
			if (ret == 1) {
				Gtk::MessageDialog dialog(*this, "Acceso restringido!");
				dialog.set_secondary_text("Contraseña invalida.");
				dialog.run();
			}
		}
		dialog->hide();
	} else
		pass = true;

	if (pass) {
		string path;
		if (Connection::getInstance()->getType() == 'I')
			path = "glade/instructor/class.glade";
		else
			path = "glade/student/class.glade";
		Glib::RefPtr<Gtk::Builder> builder = Gtk::Builder::create_from_file(path);
		ClassWindow* cw = 0;
		builder->get_widget_derived("window1", cw);
		cw->setClass(*c);

		//No se puede editar el id:
		Gtk::Entry *input;
		builder->get_widget("entry1", input);
		input->set_sensitive(false);

		cw->show();
	}
}


/**
 *
 */
void CourseWindow::onShow() {
	Gtk::Entry *input;
	builder->get_widget("entry1", input);
	std::ostringstream sout;
	sout << c.getId();
	input->set_text(sout.str());
	builder->get_widget("entry3", input);
	input->set_text(c.getName());
	builder->get_widget("entry2", input);
	input->set_text(c.getStartDate());
	builder->get_widget("entry4", input);
	input->set_text(c.getDuration());
	builder->get_widget("entry5", input);
	input->set_text(c.getPassword());
	Gtk::TextView *textView;
	builder->get_widget("textview1", textView);
	textView->get_buffer()->set_text(c.getDescription());
}


/**
 *
 */
void CourseWindow::onSave() {
	string id, name, startDate, duration, password, description;
	Gtk::Entry *input;
	builder->get_widget("entry1", input);
	id = input->get_text();
	builder->get_widget("entry3", input);
	name = input->get_text();
	builder->get_widget("entry2", input);
	startDate = input->get_text();
	builder->get_widget("entry4", input);
	duration = input->get_text();
	builder->get_widget("entry5", input);
	password = input->get_text();
	Gtk::TextView *textView;
	builder->get_widget("textview1", textView);
	description = textView->get_buffer()->get_text();

	c.setId(atol(id.c_str()));
	c.setName(name);
	c.setStartDate(startDate);
	c.setDuration(duration);
	if (password.size() > 0)
		c.setHasPassword(true);
	else
		c.setHasPassword(false);
	c.setPassword(password);
	c.setDescription(description);

	Connection::getInstance()->createCourse(c);

	this->hide();
}


/**
 *
 */
void CourseWindow::setCourse(Course c) {
	this->c = c;
	setTreeModelInfo();
}
